<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>重置密码</span>
    </div>
    <!-- 表单 -->
    <el-form :model="pwdForm" :rules="pwdFormRules" ref="pwdFormRef" label-width="100px">
      <el-form-item label="原密码" prop="old_pwd">
        <el-input v-model="pwdForm.old_pwd" type="password" minlength='6' maxlength='15'></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input v-model="pwdForm.new_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="re_pwd">
        <el-input v-model="pwdForm.re_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updatePwd">修改密码</el-button>
          <!-- 实现表单重置  resetFields()  函数-->
          <!-- 对整个表单进行重置，将所有字段值重置为初始值并移除校验结果 -->
        <el-button @click="$refs.pwdFormRef.resetFields()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: 'UserPwd',
  data() { 
       // 查看原密码 新密码 是否相同  参数 value  新密码框
           let samePwd = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        } else if (value === this.pwdForm.old_pwd) {
          callback(new Error('新旧密码不能相同!'));
        } else {
          callback();
        }
      }
         //检测 两次输入密码 是否相同
      let rePwd = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请确认密码'));
        } else if (value !== this.pwdForm.new_pwd) {
          callback(new Error('两次密码不一致!'));
        } else {
          callback();
        }
      }
    return {
      // 表单的数据对象
      pwdForm: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: ''
      },
      // 表单的验证规则对象
      pwdFormRules: {
        old_pwd:[
         { required: true, message: '请输入密码', trigger: 'blur' },
        { pattern:/^\S{6,15}$/ ,  message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        new_pwd:[
          //  { required: true, message: '请输入新密码', trigger: 'blur' },
           { pattern:/^\S{6,15}$/ ,  message: '长度在 6 到 15 个字符', trigger: 'blur' },
          //  validator验证器，可以对格式进行复杂限制
            { validator: samePwd, trigger: 'blur' }
        ],
        re_pwd:[
          //  { required: true, message: '请确认密码', trigger: 'blur' },
           { pattern:/^\S{6,15}$/ ,  message: '长度在 6 到 15 个字符', trigger: 'blur' },
           {validator:rePwd,trigger:'blur'}
        ]
      }
    }
  },
   methods: {
       updatePwd(){
     //表单校验
     this.$refs.pwdFormRef.validate(
         async (val)=>{
        // console.log(val)
        if(!val)    return  
        //请求成功
        const {data:res} =await this.$http.patch('/my/updatepwd',this.pwdForm)
        // console.log(res);
        if(res.code!==0) return this.$message.error(res.message)
         this.$message.success(res.message)
         //重置表单
         this.$refs.pwdFormRef.resetFields()
     })
  }
   }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 500px;
}
</style>